#!/bin/bash
# Date: Mon Jan 10 16:54:03 2022
# Author: January


root_path=$(pwd)
function genkey() {
    $root_path/keytool --type sm2 --mode genkey
}


function add_uav() {
    cd $root_path
    if [ ! -d keystore ];then
        echo "please init a UAV system keystore first."
        exit -1
    fi
    cd keystore
    # 无人机编号作为文件夹名称
    uav_id=$1
    if [ -d $uav_id ];then
        echo "$uav_id alreafidy exists"
        return
    fi
    mkdir $uav_id
    cd $uav_id
    mkdir self
    genkey
    mv sm2.key self/sm2.key
    mv sm2.pub self/sm2.pub
    mkdir gs
    # 将gs的公钥加入到新的uav中
    cp ../gs/self/sm2.pub gs/sm2.pub
    # 将uav的公钥加入到gs中
    [ -d ../gs/uav/$uav_id ] || mkdir ../gs/uav/$uav_id
    cp self/sm2.pub ../gs/uav/$uav_id/sm2.pub
    echo "uav [$uav_id] is created"
}

function init_uas_keystore() {
    cd $root_path
    [ -d keystore ] || mkdir keystore
    cd keystore
    [ -d gs ] || mkdir gs
    cd gs
    [ -d self ] || mkdir self
    genkey
    mv sm2.key self/sm2.key
    mv sm2.pub self/sm2.pub
    echo "UAS keystore inited"
}

init_uas_keystore
add_uav 1
add_uav 2
add_uav 3
